package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.SysRole;
import com.jy.wms.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SysRoleController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author syh
 * @Date Tue Aug 29 16:22:52 CST 2017
 */
@Controller
@RequestMapping("/sysrole")
@Api(value = "/sysrole", description = "关于角色管理操作")
public class SysRoleController extends BaseController<SysRoleService,SysRole> {

    @PostMapping(value="/list")
    @ApiOperation(value = "角色管理 POST请求查询角色信息",response = JSONObject.class, notes = "POST请求查询角色信息")
    public JSONObject getSysRole(@RequestBody SysRole entity) {
        Page<SysRole> list = new Page<SysRole>();
        JSONObject json = getJSONObject();
        try {
            list=baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","角色信息",e.getMessage(),"");
        }
        return json;
    }
    @PostMapping(value = "/add")
    @ApiOperation(value = "角色信息 新增角色信息",response = JSONObject.class, notes = "POST新增角色信息")
    public  JSONObject addSysRole(@RequestBody SysRole entity) {
        JSONObject json = getJSONObject();
        try{
            SysRole entitys=baseService.searchAdd(entity.getMc());
            if(entitys!=null){
                json = SetStatus.setFunctionType(1,"warning","角色信息","新增角色名称重复","");
            }else{
                entity.setId(UUIDUtils.uuid());
                    baseService.baseInsert(entity);
                json = SetStatus.setFunctionType(1,"success","角色信息","角色信息添加成功", JSONObject.fromObject(entity).toString());

            }
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"warning","角色信息",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","角色信息","角色信息添加失败","");
        }
        return json;
     }

    @GetMapping (value = "/getSysRole/{id}")
    @ApiOperation(value = "角色信息 查看单个角色信息",response = JSONObject.class, notes = "GET查看单个角色信息")
    public  JSONObject getSysRoleById(@PathVariable("id") String id) {
        JSONObject json = getJSONObject();
        SysRole entity= null;
        try {
            entity = baseService.baseSelectByPrimaryKey(id);
            json= JSONObject.fromObject(entity);//传对象
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","角色信息","查看单个角色信息失败","");
        }
        return json;

    }
    @PostMapping(value = "/edit")
    @ApiOperation(value = "角色信息 编辑角色信息",response = JSONObject.class, notes = "POST编辑角色信息")
    public JSONObject editSysRole(@RequestBody SysRole entity) {
        JSONObject json = getJSONObject();
        try {
            SysRole entitys=baseService.searchAdds(entity.getMc(),entity.getId());
            if(entitys!=null){
                json = SetStatus.setFunctionType(1,"warning","角色信息","角色名称重复","");
            }else {
                baseService.baseUpdateByPrimaryKeySelective(entity);
                json = SetStatus.setFunctionType(1,"success","角色信息","角色信息编辑成功", JSONObject.fromObject(entity).toString());

            }
                }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"warning","角色信息",e.getMessage(),"");

        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","角色信息","角色信息编辑失败","");
        }
        return json;
    }

    /**
     * 授权编辑
     * @param id
     * @return
     */
    @GetMapping(value = "/getSysRoleFunction/{id}")
    @ApiOperation(value = "角色编辑授权功能信息 查看单个角色功能信息",response = JSONObject.class, notes = "GET查看单个角色功能信息")
    public  JSONObject getSysRoleFunctionById(@PathVariable("id") String id) {
        JSONObject json = getJSONObject();
        try {
            List<String> list = baseService.getSysRoleFunctionById(id);
            json.put("ids",list);//往前台传键值对
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            json = SetStatus.setFunctionType(1, "error", "角色编辑授权功能信息", "查看角色功能信息失败", "");
        }
        return json;
    }

    /**
     * 新增授权方法
     * @param map
     * @return
     */
    @PostMapping(value = "/addShouquan")
    @ApiOperation(value = "角色新增授权功能信息 新增授权功能信息",response = JSONObject.class, notes = "POST新增授权功能信息")
    public  JSONObject addSysRoleShouquan(@RequestBody Map<String,Object> map) {
        JSONObject json = getJSONObject();
        String id=(String) map.get("id");

        /*查全部菜单和功能*/
        try{
             baseService.getFunctionMenu(map);//操作所有菜单和功能的授权
        }catch (SucessException e){
            json = SetStatus.setFunctionType(1,"success","角色新增授权功能信息",e.getMessage(),"");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"warning","角色新增授权功能信息",e.getMessage(),"");
        }
        return json;
    }
}


